/******************************************************************************

    Copyright (C) 2006 Alessio Stalla (alessiostalla@gmail.com)

    This file is part of PrettyProlog.

    PrettyProlog is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    PrettyProlog is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PrettyProlog; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    See the file COPYING for further details.

 *****************************************************************************/

package prettyprolog.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * An instance of PrettyProlog running in non-applet mode.<br />
 * The main() method can handle the following command line arguments:
 * <ul>
 * <li>&lt;theory&gt; - a path to a file containing a theory to load on startup.
 * </li>
 * </ul>
 */
public class PrettyPrologApp extends JFrame {

	private static final long serialVersionUID = 1L;
	private PrettyPrologApplet applet;
	protected final int DEFAULT_WIDTH = 800, DEFAULT_HEIGHT = 600;
	private StatusBar status_bar = new StatusBar();

	/** Constructs a new PrettyPrologApp. */
	@SuppressWarnings("serial")
	public PrettyPrologApp() {
		super();

		applet = new PrettyPrologApplet() {

			public void showStatus(String msg) {
				status_bar.setText(msg);
			}

		};
		applet.isApplet(false);

		JMenuBar menubar = applet.getJMenuBar();
		JMenu file_menu = menubar.getMenu(PrettyPrologApplet.FILE_MENU);
		if (file_menu.getItemCount() > 0)
			file_menu.addSeparator();
		/*
		 * JMenuItem file_test = new JMenuItem("Test add/remove panel");
		 * file_test.addActionListener(new ActionListener() { private
		 * PrettyPrologPanel pnl = null;
		 * 
		 * public void actionPerformed(ActionEvent e) { if(pnl == null) { pnl =
		 * applet.getPanels().get(3); applet.removePanel(pnl); } else {
		 * applet.addPanel(pnl, PrettyPrologApplet.PanelPosition.RIGHT); pnl =
		 * null; } } }); file_menu.add(file_test);
		 */
		final JMenuItem file_exit = new JMenuItem("Exit");
		file_exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		file_exit.addChangeListener(new ChangeListener() {

			public void stateChanged(ChangeEvent e) {
				if (file_exit.isArmed())
					status_bar.setText("Closes PrettyProlog.");
				else
					status_bar.clear();
			}

		});

		file_menu.add(file_exit);

		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(status_bar, BorderLayout.SOUTH);
		getContentPane().add(applet);
		applet.init();
		setTitle("PrettyCLP");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}

	/** Loads a Theory from a file. */
	public void loadTheory(String filename) {
		try {
			applet.loadTheory(new java.io.File(filename));
		} catch (Exception _e) {
			_e.printStackTrace();
			JOptionPane.showMessageDialog(this, "Error: can't load theory ("
					+ _e + ")", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}

	/** @return the PrettyPrologApplet associated to this frame. */
	public PrettyPrologApplet getApplet() {
		return applet;
	}

}